"""
python fujian1_batch_pmdarima_forecast.py

"""

import os
import logging
from fujian1_pmdarima_single_category import predict_and_save_inventory
# from fujian1_auto_pmdarima_single_category import predict_and_save_inventory

# 设置输入和输出路径（相对路径）
input_dir = r"..\fujian\fujian1\spline_then_smooth"
output_dir = r"..\fujian\fujian1\pmdarima_single_batch"

# 创建输出目录（如果不存在）
os.makedirs(output_dir, exist_ok=True)

# 配置日志
log_file = os.path.join(output_dir, 'batch_forecast.log')
logging.basicConfig(filename=log_file, level=logging.WARNING,
                    format='%(asctime)s - %(levelname)s - %(message)s')

def process_all_files():
    # 遍历输入目录中的所有 JSON 文件
    json_files = [f for f in os.listdir(input_dir) if f.endswith('.json')]
    total_files = len(json_files)

    for idx, filename in enumerate(json_files, 1):
        input_path = os.path.join(input_dir, filename)
        output_path = os.path.join(output_dir, filename)

        try:
            # 调用预测函数
            predict_and_save_inventory(input_path, output_path)
            print(f"{idx}/{total_files}: Processed {filename} successfully.")
        
        except Exception as e:
            # 如果发生错误，则记录日志并继续处理下一个文件
            logging.warning(f"Error processing {filename}: {e}")
            print(f"{idx}/{total_files}: Failed to process {filename}. Check logs for details.")

if __name__ == "__main__":
    process_all_files()
